/**
 * @author zhoushengtao
 * @since 2012-8-29 下午9:12:43
 */

package com.stchou.superbrowser.activity;

import com.stchou.superbrowser.db.WebsiteOpenHelper;
import com.stchou.superbrowser.db.bean.website;
import com.stchou.superbrowser.ui.adapter.WebsiteListviewAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.List;

public class InputWebsiteActivity extends Activity
{
    private EditText m_editText;
    private Button m_clearButton;
    private Button m_searchButton;
    private ListView m_listView;
    private WebsiteOpenHelper m_DBhelper;
    private List<website> m_websiteLists;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inputwebsite);
        initViews();
    }

    private void initViews()
    {
        m_editText = (EditText)findViewById(R.id.super_edit);
        m_editText.addTextChangedListener(new TextWatcher()
        {
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
            {
                String edit_content = m_editText.getText().toString();
                if (edit_content.equals(""))
                    return;
                getData(edit_content);
                m_listView.setAdapter(new WebsiteListviewAdapter(InputWebsiteActivity.this, m_websiteLists));

            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
            {
            }

            public void afterTextChanged(Editable arg0)
            {
            }
        });
        m_clearButton = (Button)findViewById(R.id.button_hide);
        m_clearButton.setOnClickListener(new OnClickListener()
        {
            public void onClick(View arg0)
            {
                m_editText.setText("");
            }
        });

        m_listView = (ListView)findViewById(R.id.listView);
        m_listView.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
            }
        });
        m_searchButton = (Button)findViewById(R.id.search_button);
        m_searchButton.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                finish();
            }
        });

    }

    private void getData(String text)
    {
        m_DBhelper = new WebsiteOpenHelper(this, WebsiteOpenHelper.DB_NAME, null, WebsiteOpenHelper.DB_VERSION);
        m_websiteLists = m_DBhelper.query(text);
    }

}
